<?php
/**
 * Permission Helper
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Tisa Pathumwan
 * 
 * 1 = RA = Root Admin
 * 2 = GA = General Admin
 * 3 = MB = Member
 * 
 */

// ------------------------------------------------------------------------
function getUserSession(){
	// Check login session first
	$userSession = new CI_Session();
	return $userSession->userdata('userSession');
}

function getSessionUserAid(){
	$user = getUserSession();
	return get_array_value($user,"aid","0");
}
function getSessionUserCid(){
	$user = getUserSession();
	return get_array_value($user,"username","");
}
function getSessionUserRoleAid(){
	$user = getUserSession();
	return get_array_value($user,"user_role_aid","");
}
function getSessionUserStatus(){
	$user = getUserSession();
	return get_array_value($user,"status","");
}

function is_login(){
	$user_aid = getSessionUserAid();
	if(empty($user_aid)) {
		$userSession = new CI_Session();
		$redirectUrlSession = new CI_Session();
		$userSession->set_userdata('redirectUrlSession', uri_string());
		redirect("/login");
	}
}
function is_root_admin(){
	$user_role_aid = getSessionUserRoleAid();
	if($user_role_aid == 1) return true;
	else return false;
}
function is_general_admin(){
	$user_role_aid = getSessionUserRoleAid();
	if($user_role_aid == 2) return true;
	else return false;
}
function is_member_admin(){
	$user_role_aid = getSessionUserRoleAid();
	if($user_role_aid == 3) return true;
	else return false;
}
function is_agent(){
	$user_role_aid = getSessionUserRoleAid();
	if($user_role_aid == 5) return true;
	else return false;
}


function for_root_admin(){
	$user = getUserSession();
	$role_can_access = array("1");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}
function for_root_admin_or_higher(){
	$user = getUserSession();
	$role_can_access = array("1");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}

function for_general_admin(){
	$user = getUserSession();
	$role_can_access = array("2");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}
function for_general_admin_or_higher(){
	$user = getUserSession();
	$role_can_access = array("1","2");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}

function for_member(){
	$user = getUserSession();
	$role_can_access = array("3");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}
function for_member_or_higher(){
	$user = getUserSession();
	$role_can_access = array("1","2","3");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}

function for_internal_staff(){
	$user = getUserSession();
	$role_can_access = array("1", "2", "3", "4");
	return is_match_role(getSessionUserRoleAid(),$role_can_access);
}
?>